if (!Travian) {

	var Travian = {
		travianDoc : null,
		toolDoc : null
	};

	Travian.createToolPaneLink = function(href, html) {
		var a = Travian.toolDoc.$C("a");
		a.href = 'javascript:;';
		a.innerHTML = html;
		a.title = href;
		a.addEventListener('click', function() {
			Travian.travianDoc.ownerWindow.location.href = href;
		}, false);
		return a;
	}
	Travian.refreshUsefulLinksPane = function() {
		var br = Travian.travianDoc.evaluateXPath('//table[@id="navi_table"]//br')[1];
		if (!br) return;
		while(br.previousSibling&&br.previousSibling.tagName.toUpperCase()!='BR'){
			br.parentNode.removeChild(br.previousSibling);
		}
		TrvnUtils.each(TrvnCfg.getCurrentAccount().usefulLinks, function(index, link) {
			var a = Travian.travianDoc.$C('a');
			a.innerHTML = link.title;
			a.href = link.link;
			br.parentNode.insertBefore(a, br);
		});
	}

	Travian.createEventPane = function() {
		var pane = Travian.toolDoc.createListPane("evnt", "事件", true);
		var btn = Travian.toolDoc.$C("input");
		btn.id = "alertOnTaskFinish";
		btn.type = "checkbox";
		btn.checked = !TrvnCfg.alertOnTaskFinish;
		btn.addEventListener('click', function() {
			TrvnCfg.alertOnTaskFinish = !this.checked;
		}, false);
		pane.appendChild(btn);
		var span = Travian.toolDoc.$C("span");
		span.addEventListener('click', function() {
			btn.checked = !btn.checked;
			TrvnCfg.alertOnTaskFinish = !TrvnCfg.alertOnTaskFinish;
		}, false);
		span.style.cursor = "default";
		span.style.fontSize = "smaller";
		span.innerHTML = "任务完成时不弹出提示";
		pane.appendChild(span);
		return pane;
	}
	Travian.createNotePad = function() {
		var pane = Travian.toolDoc.createPane("草稿纸", "notePad");
		var np = Travian.toolDoc.$C("textarea");
		np.style.width = "98%";
		np.rows = "5";
		pane.lastChild.appendChild(np);
		var button = Travian.toolDoc.$C("button");
		button.type = "button";
		button.addEventListener('click', function() {
			np.value = "";
		}, false);
		button.innerHTML = "清空";
		pane.appendChild(button);
		return pane;
	}
	Travian.createConfigPane = function() {
		var configDiv = Travian.toolDoc.$C("div");
		configDiv.id = "configPane";
		configDiv.style.border = "1px solid #00BC00";
		configDiv.style.display = "none";

		var pane = Travian.toolDoc.createPane("基本设置", "basicConfig");
		// var contentDiv = pane.firstChild.nextSibling;
		configDiv.appendChild(pane);

		return configDiv;
	}
	Travian.createTools = function() {
		var body = Travian.toolDoc.$C("div");
		body.id = "TrvnTool";
		Travian.toolDoc.body.appendChild(body);
		body.appendChild(Travian.createConfigPane());
		var toolPaneDiv = Travian.toolDoc.$C("div");
		toolPaneDiv.id = "toolPane";
		body.appendChild(toolPaneDiv);
		// 初始化登录表单
		toolPaneDiv.appendChild(TrvnLoginManager.init());
		toolPaneDiv.appendChild(Travian.createEventPane());
		toolPaneDiv.appendChild(Travian.toolDoc.createListPane("history", "历史记录", true));
		// toolPaneDiv.appendChild(Travian.createNotePad());
		if (TrvnCfg.enableLog) {
			toolPaneDiv.appendChild(TrvnUtils.Logger.create());
		}
	}

	Travian.getPromptService = function() {
		if (!Travian.promptService) {
			Travian.promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
		}
		return Travian.promptService;
	}
	Travian.promptPassword = function() {
		var password = {
			value : ''
		};
		var check = {
			value : true
		};
		if (Travian.getPromptService().promptPassword(null, TrvnCfg.description, "输入密码:", password, null, check)) return password.value;
		else return null;
	}
	Travian.promptUsernameAndPassowrd = function() {
		var username = {
			value : ""
		};
		var password = {
			value : ""
		};
		var check = {
			value : TrvnCfg.promptForPasswordEveryTime
		};
		var result = false;
		if (TrvnCfg.promptForPasswordEveryTime) {
			result = Travian.getPromptService().prompt(null, TrvnCfg.description, "输入用户名:", username, null, check);
		} else {
			result = Travian.getPromptService().promptUsernameAndPassword(null, TrvnCfg.description, "输入用户名和密码:", username, password, null, check);
		}
		if (result) return {
			username : username.value,
			password : password.value
		};
		else return null;
	}
	Travian.selectServer = function() {
		var servers = [], selected = {};
		TrvnUtils.each(TrvnCfg.servers, function(id, server) {
			servers[id] = server.title + '(' + server.base + ')';
		});
		if (Travian.getPromptService().select(null, TrvnCfg.description, "选择帐号所在的服务器", servers.length, servers, selected)) {
			return selected;
		} else return false;
	}

	Travian.gPrefsWrapper = new PrefsWrapper("extensions.traviants.");
	Travian.changeConfigFile = function() {
		try {
			Travian.gPrefsWrapper.setCharPref("configPath", "");
			var path = TrvnCfgManager.reload();
			if (path) {
				Travian.gPrefsWrapper.setCharPref("configPath", encodeURIComponent(path));
			}
			if (Travian.toolDoc) {
				TrvnLoginManager.init();
			}
		} catch (e) {
			alert("exception 105: " + e);
		}
	}
	Travian.setToolPanePosition = function(pos, force) {
		try {
			if (TrvnCfg.toolPanePosition == pos && !force) return;
			var appContentNode = document.getElementById('appcontent');
			switch (pos) {
				case 'top' :
					appContentNode.insertBefore(Travian.contentSplitter, appContentNode.firstChild);
					appContentNode.insertBefore(Travian.contentBox, appContentNode.firstChild);
					TrvnCfg.toolPanePosition = pos;
					break;
				case 'bottom' :
					appContentNode.appendChild(Travian.contentSplitter);
					appContentNode.appendChild(Travian.contentBox);
					TrvnCfg.toolPanePosition = pos;
					break;
				case 'left' :
					appContentNode.parentNode.insertBefore(Travian.contentBox, appContentNode);
					appContentNode.parentNode.insertBefore(Travian.contentSplitter, appContentNode);
					TrvnCfg.toolPanePosition = pos;
					break;
				case 'right' :
					appContentNode.parentNode.appendChild(Travian.contentSplitter);
					appContentNode.parentNode.appendChild(Travian.contentBox);
					TrvnCfg.toolPanePosition = pos;
					break;
			}
			setTimeout(Travian.initTravian, 500);
		} catch (e) {
			alert("travian.js 408: " + e);
		}
	}
	Travian.toggleToolPane = function(force) {
		Travian.toolePaneVisible = force == undefined ? (Travian.toolePaneVisible ? false : true) : force;
		Travian.contentBox.setAttribute('collapsed', !Travian.toolePaneVisible);
		Travian.contentSplitter.setAttribute('collapsed', !Travian.toolePaneVisible);
	}
	Travian.initToolPane = function() {
		Travian.toolDoc = document.getElementById('travianToolFrame').contentWindow.document;
		Travian.toolDoc.body.innerHTML = "";
		TrvnUtils.initDoc(Travian.toolDoc);
		var css = Travian.toolDoc.$C("link");
		css.rel = "stylesheet";
		css.type = "text/css";
		css.href = TrvnData.url.chromHome + "/css/unx.css";
		Travian.toolDoc.getElementsByTagName("head")[0].appendChild(css);
		Travian.createTools();
		Travian.toggleToolPane(true);
	}
	Travian.refreshAllVillages = function(){
	}
	Travian.closeTravian = function() {
		if (!Travian.initialized) return;
		try {
			TrvnCfgManager.save();
			TrvnCfgManager.stopSaveConfigTimer();
			TrvnTaskManager.stopScanTasksInterval();
			window.removeEventListener("close", Travian.closeTravian, false);
			Travian.toggleToolPane(false);
			Travian.browser = null;
			Travian.initialized = false;
		} catch (e) {
			alert("travian.js 407: " + e);
		}
	}
	Travian.initTravian = function() {
		try {
			Travian.initToolPane();
			if (Travian.browser && Travian.browser.IS_TRAVIAN) {
				Travian.initialized = true;
				return;
			}
			TrvnTaskManager.scanTasks();
			TrvnTaskManager.startScanTasksInterval();
			Travian.browser = gBrowser.selectedBrowser;
			Travian.browser.IS_TRAVIAN = true;
			Travian.browser.addEventListener("load", function(event) {
				try {
					if(Travian.browser.contentDocument.intercepted) return;
					TrvnCfgManager.scheduleSaveConfig();
					Travian.travianDoc = Travian.browser.contentDocument;
					TrvnUtils.initDoc(Travian.travianDoc);
					Travian.travianDoc.ownerWindow = Travian.browser.contentWindow;
					var title = Travian.travianDoc.evaluateXPath('//h1')[0];
					title = title ? title.innerHTML : "";
					Travian.travianDoc.pageTitle = title;
					TrvnInterceptorService.intercept(Travian.travianDoc);
					Travian.travianDoc.intercepted = true;
				} catch (e) {
					TrvnUtils.Logger.error("travian.js 403: " + e);
				}
			}, true);
			Travian.travianDoc = Travian.browser.contentWindow.document;
			TrvnUtils.initDoc(Travian.travianDoc);
			Travian.travianDoc.ownerWindow = Travian.browser.contentWindow;
			Travian.travianDoc.pageTitle = "";
			gBrowser.tabContainer.addEventListener("TabClose", function(event) {
				if (event.target.linkedBrowser.IS_TRAVIAN) {
					Travian.closeTravian();
				}
			}, false);
			window.addEventListener("close", Travian.closeTravian, false);
			Travian.initialized = true;
		} catch (e) {
			alert("travian.js 406: " + e);
		}
	}
	Travian.startGame = function(event) {
		try {
			if (event && event.button != 0) return;
			if (Travian.initialized) {
				Travian.toggleToolPane();
				return;
			}
			var path = TrvnCfgManager.init(decodeURIComponent(Travian.gPrefsWrapper.getCharPref("configPath")));
			if (path) {
				Travian.gPrefsWrapper.setCharPref("configPath", encodeURIComponent(path));
			} else {
				throw "请提供配置文件的位置先！";
			}
			Travian.contentBox = document.getElementById('trvnContentBox');
			Travian.contentSplitter = document.getElementById('trvnContentSplitter');
			Travian.setToolPanePosition(TrvnCfg.toolPanePosition, true);
		} catch (e) {
			alert("travian.js 406: " + e);
		}
	}

}// end if(!Travian)
